<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="投保人名称" prop="toubaoId">
        <el-select v-model="queryParams.toubaoId" placeholder="请选择投保人名称" clearable>
          <el-option
              v-for="dict in taobaoList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="被保险人名称" prop="beibaoId">
        <el-select v-model="queryParams.beibaoId" placeholder="请选择被保险人名称" clearable>
          <el-option
              v-for="dict in taobaoList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="保险人名称" prop="baoxianId">
        <el-select v-model="queryParams.baoxianId" placeholder="请选择保险人名称" clearable>
          <el-option
              v-for="dict in baoxianList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="经纪/代理公司名称" prop="proxyId" label-width="130px">
        <el-select v-model="queryParams.proxyId" placeholder="请选择经纪/代理公司名称" clearable>
          <el-option
              v-for="dict in proxyList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="险种" prop="insureType" label-width="50">
        <el-select v-model="queryParams.insureType" placeholder="险种" clearable>
          <el-option
              v-for="dict in dict.type.insure_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="success"-->
      <!--    plain-->
      <!--    icon="el-icon-edit"-->
      <!--    size="mini"-->
      <!--    :disabled="single"-->
      <!--    @click="handleUpdate"-->
      <!--    v-hasPermi="['insurance:risk:edit']"-->
      <!--  >修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="danger"-->
      <!--    plain-->
      <!--    icon="el-icon-delete"-->
      <!--    size="mini"-->
      <!--    :disabled="multiple"-->
      <!--    @click="handleDelete"-->
      <!--    v-hasPermi="['insurance:risk:remove']"-->
      <!--  >删除</el-button>-->
      <!--</el-col>-->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="riskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="保单号码" align="center" prop="insureNo"/>
      <el-table-column label="合同名称" align="center" prop="contractName"/>
      <el-table-column label="风险可能性" align="center" prop="possibility">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.possibility" :value="scope.row.possibility"/>
        </template>
      </el-table-column>
      <el-table-column label="风控服务计划售前" align="center" prop="front">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no_number" :value="scope.row.front"/>
        </template>
      </el-table-column>
      <el-table-column label="风控服务计划售后频率" align="center" prop="back">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.back" :value="scope.row.back"/>
        </template>
      </el-table-column>
      <el-table-column label="标的主要类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <!--<el-table-column label="气象数据库附件" align="center" prop="attachment" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="230px" fixed="right">
        <template slot-scope="scope">
          <el-button
              v-if="!scope.row.id"
              type="text"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd(scope.row)"
              v-hasPermi="['insurance:risk:add']"
          >新增
          </el-button>
          <el-button
              v-if="scope.row.id"
              size="mini"
              type="text"
              @click="goToChildrenPage(scope.row,'/views/insurance/report/index')"
          >风控报告
          </el-button>
          <el-button
              v-if="scope.row.id"
              size="mini"
              type="text"
              @click="goToChildrenPage(scope.row,'/views/insurance/expert/index')"
          >风控专家
          </el-button>
          <el-button
              v-if="scope.row.id"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['insurance:risk:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              v-if="scope.row.id"
              type="text"
              icon="el-icon-delete"
              style="color: red"
              @click="handleDelete(scope.row)"
              v-hasPermi="['insurance:risk:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改保险风控信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!--<el-form-item label="所属保险id" prop="insureId">-->
        <!--  <el-input v-model="form.insureId" placeholder="请输入所属保险id" />-->
        <!--</el-form-item>-->
        <el-form-item label="风险可能性" prop="possibility">
          <el-select v-model="form.possibility" placeholder="请选择风险可能性">
            <el-option
                v-for="dict in dict.type.possibility"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="风控服务计划售前" prop="front">
          <el-select v-model="form.front" placeholder="请选择风控服务计划售前">
            <el-option
                v-for="dict in dict.type.yes_no_number"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="风控服务计划售后频率" prop="back">
          <el-select v-model="form.back" placeholder="请选择风控服务计划售后频率">
            <el-option
                v-for="dict in dict.type.back"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标的主要类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择标的主要类型">
            <el-option
                v-for="dict in dict.type.type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="气象数据库附件" prop="attachment">
          <file-upload v-model="form.attachment" :fileType="['pdf']" :fileSize="99999" :limit="99999"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listRisk, getRisk, delRisk, addRisk, updateRisk} from "@/api/insurance/risk";
import {getBaoxianlist, getDaililist, getToubaolist} from "@/api/common/common";

export default {
  name: "Risk",
  dicts: ['yes_no_number', 'back', 'possibility', 'type','insure_type'],
  data() {
    return {
      taobaoList: [],
      proxyList: [],
      baoxianList: [],
      touBaoList: [],
      beibaoList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险风控信息表格数据
      riskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        insureId: null,
        toubaoId: null,
        beibaoId: null,
        baoxianId: null,
        proxyId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.initData()
    this.getList();
  },
  methods: {
    initData() {
      // getToubaolist().then().then(res => {
      //   res.data.forEach(item => {
      //     item.deptId = String(item.deptId)
      //   });
      //   this.touBaoList = res.data
      // })
      getToubaolist().then().then(res => {
        this.taobaoList = res.data
      })
      getBaoxianlist().then().then(res => {
        this.baoxianList = res.data
      })
      getDaililist().then().then(res => {
        this.proxyList = res.data
      })
      // getFengkonglist().then().then(res => {
      //   res.data.forEach(item => {
      //     item.deptId = String(item.deptId)
      //   });
      //   this.beibaoList = res.data
      // })
      // getBaoxianlist().then().then(res => {
      //   res.data.forEach(item => {
      //     item.deptId = String(item.deptId)
      //   });
      //   this.baoxianList = res.data
      // })
      // getDaililist().then().then(res => {
      //   res.data.forEach(item => {
      //     item.deptId = String(item.deptId)
      //   });
      //   this.proxyList = res.data
      // })
    },
    /** 查询保险风控信息列表 */
    getList() {
      this.loading = true;
      listRisk(this.queryParams).then(response => {
        this.riskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        insureId: null,
        possibility: null,
        front: null,
        back: null,
        type: null,
        attachment: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form.insureId = row.insureId
      this.open = true;
      this.title = "添加保险风控信息";
    },
    goToChildrenPage(row,path){
      this.$router.push(
          {
            path: path,
            query: {
              'insureId': row.insureId,
              'riskId': row.id
            }
          }
      );
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.form.insureId = row.insureId
      getRisk(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保险风控信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRisk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRisk(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的保险风控信息？').then(function () {
        return delRisk(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('insurance/risk/export', {
        ...this.queryParams
      }, `risk_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
